/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "ParticleManager.h"
#include "../Kernel.h"

extern Kernel* g_kernel;

#define TIME (g_kernel->GetTime())

ParticleManager::ParticleManager(void)
{
    Register();
}

ParticleManager::~ParticleManager(void)
{
    UnRegister();
}


/** Update the Emitters */
void ParticleManager::Update(long dt)
{
    type_Emit::iterator it = m_emitters.begin();
    while( it != m_emitters.end() )
    {
        Emitter *e = (*it);
        if ( TIME > e->LifeSpanOfEmitter )
        {
            delete e;
            it = m_emitters.erase( it );
        }
        else
        {
            e->Update( dt );
            ++it;
        }
    }
}

/** Render the Emitters */
void ParticleManager::Render(IRender* r, ICamera* cam)
{
    type_Emit::iterator it = m_emitters.begin();
    for(; it != m_emitters.end(); it++ )
    {
        (*it)->Render( r, cam );
    }
}

/** Remove an emitter */
void ParticleManager::RemoveEmitter( Emitter *e )
{
    type_Emit::iterator it = m_emitters.begin();
    for(; it != m_emitters.end(); it++ )
    {
        if ( (*it)->GetID() == e->GetID() )
        {
            m_emitters.erase( it );
            delete e;
            return;
        }
    } 
}

/** Handle an event */
bool ParticleManager::HandleEvent( IEvent* e )
{
    bool success = false;
    switch( e->GetType() )
    {
    case EVT_ADD_PARTICLE:
        AddEmitter( e->GetData<ParticleData>()->m_emitter );
		success = true;
        break;

    case EVT_RM_PARTICLE:
        RemoveEmitter( e->GetData<ParticleData>()->m_emitter );
		success = true;
        break;
    default:
        break;
    
    };
    return success;
}